#include "form_view_procedure.h"
#include "ui_form_view_procedure.h"

// add nacessary includes here
#include "utils/upgrade/i_upgrade_bll.h"
#include "utils/database/database_bll_t1c.h"
#include "ui/share/form_prompt_dlg.h"
#include "ui/factory/extract_debug/mvc/extract_table_view.h"


FormViewProcedure::FormViewProcedure(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormViewProcedure)
{
    ui->setupUi(this);
}

FormViewProcedure::~FormViewProcedure()
{
    qDebug() << Q_FUNC_INFO;
    delete ui;

    if(m_pMyTableView) {
        delete m_pMyTableView;
        m_pMyTableView = nullptr;
    }
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormViewProcedure" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormViewProcedure" class
 */
FormViewProcedure* FormViewProcedure::getInstance(QWidget *pParent)
{
    static FormViewProcedure *pNew = nullptr;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormViewProcedure(pParent);
        if(nullptr == pNew) return nullptr;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Initialize control's UI
        pNew->_initUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormViewProcedure::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI()), this, SLOT(slotRefreshUI()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormViewProcedure::_initUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/home/homepage/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->pushButton_Cancel->setFocusPolicy(Qt::NoFocus);
}

/*
 * Function:    displayWindow
 * Description: Show window & initialize data
 * Paramer:     void
 * Return:      void
 */
void FormViewProcedure::displayWindow(int nProcedureType, int nProcedureId)
{
    qDebug() << Q_FUNC_INFO << nProcedureType << nProcedureId;
    m_nProcedureType = nProcedureType;
    m_nProcedureId = nProcedureId;

    ui->label_PageTitle->setText(tr("View a procedure"));

    if(E_PT_EXTRACT == nProcedureType) {
        ui->stackedWidget_Procedure->setCurrentIndex(0);

        // Load extract process info
        if(1 == 1) {
            if(!m_pMyTableView) m_pMyTableView = new ExtractTableView(ui->stackedWidget_Steps);
            Q_ASSERT(m_pMyTableView);

            m_pMyTableView->setFixedSize(986, 380);
            m_pMyTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
            m_pMyTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
            m_pMyTableView->clearRows();

            if(0 < nProcedureId) {
                ST_PROCESS_PARAMS stProcessParams;
                stProcessParams.nProcessId = nProcedureId;
                m_pMyTableView->loadDataFromDb(stProcessParams);

                ui->label_PageTitle->setText(stProcessParams.sProcessName);
            }
        }
    }

    this->show();
}

void FormViewProcedure::on_pushButton_Cancel_clicked()
{
    QTimer::singleShot(100, this, [&]() {
        this->hide();
    });
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		void
* Return:		void
*/
void FormViewProcedure::slotRefreshUI()
{
    qDebug() << Q_FUNC_INFO;

    ui->label_PageTitle->setText(tr("Slect a procedure"));

    // Refresh the list view data
    _refreshListView();
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *e - the pointer of "QEvent"
* Return:		void
*/
void FormViewProcedure::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);

        qDebug() << Q_FUNC_INFO;
        break;
    default:
        break;
    }

    QWidget::changeEvent(e);
}

/*
 * Function:    _refreshListView
 * Description: Refresh the list view data
 * Paramer:     void
 * Return:      void
 */
void FormViewProcedure::_refreshListView()
{

}
